package com.lc.controller;

import com.github.pagehelper.PageInfo;
import com.lc.bean.Emp;
import com.lc.service.EmpService;
import com.lc.utils.Constants;
import com.lc.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/emp")
public class EmpController {

    @Autowired
    EmpService empService;

    /**
     * @param emp
     * @param session
     * @param model
     * @return
     */
    @RequestMapping("/login")
    public String login(Emp emp, HttpSession session, Model model){
        Emp loginEmp = empService.login(emp);
        if(!ObjectUtils.isEmpty(loginEmp)) {
            // 把用户信息设置到session域中，下一个页面需要使用到用户信息
            session.setAttribute(Constants.LOGIN_USER_TAG,loginEmp);
            if(StringUtils.isEmpty(emp.getRedirectUrl())){
                return "main";
            }else{
                return "redirect:" + emp.getRedirectUrl();
            }
        } else {
            // 设置错误消息到request域中
            model.addAttribute("msg","账号或密码错误");
            return "forward:/login.jsp?redirectUrl="+emp.getRedirectUrl();
        }
    }

    @RequestMapping("list")
    @ResponseBody
    public R list(@RequestParam(defaultValue = "1") Integer pageNum,
                  @RequestParam(defaultValue = "5") Integer pageSize) {
        PageInfo<Emp> emps = empService.list(pageNum,pageSize);
        return R.ok(emps);
    }

    // 查询采购部门的领导列表  roleid=2
    @RequestMapping("/findRoleListByRoleId")
    @ResponseBody
    public R findRoleListByRoleId() {
        List<Emp> emps = empService.findRoleListByRoleId(2);
        return R.ok(emps);
    }

    // 查询跟单人列表，运输任务指派需要 roleid=6
    @GetMapping("/assignEmp")
    @ResponseBody
    public R findAssignEmp() {
        List<Emp> emps = empService.findRoleListByRoleId(6);
        return R.ok(emps);
    }

}
